package com.example.demo.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.demo.service.AuthService;
import com.google.gson.JsonObject;

/**
 * @author Lucy
 * @create 2024-03-08 12:15
 */
public class SemanticComparisonUtil {
    public static boolean isSimilar(String str1, String str2) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("word_1", str1);
        jsonObject.addProperty("word_2", str2);
        String url = " https://aip.baidubce.com/rpc/2.0/nlp/v2/word_emb_sim";
        try {
            String accessToken = AuthService.getAuth();
            String apiResult = HttpUtil.post(url, accessToken, String.valueOf(jsonObject));
            JSONObject jsonResult = JSONUtil.parseObj(apiResult);
            double score = jsonResult.getDouble("score");
            if(score > 0.5) return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
